//
//  NearTableViewController.swift
//  许鲜
//
//  Created by hbl on 16/4/22.
//  Copyright © 2016年 hbl. All rights reserved.
//

import UIKit
import AFNetworking

class NearTableViewController: UITableViewController {

    var dataArray:Array<AnyObject> = Array()
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "附近"
       // getData()
        setupTableView()
        tableView.mj_header.beginRefreshing()
    }
    
    func setupTableView(){
        tableView.registerNib(UINib.init(nibName: "NearHomeTableViewCell", bundle: nil), forCellReuseIdentifier: "cell")
        tableView.rowHeight = 250
        tableView.backgroundColor = UIColor(red: 220/255.0, green: 220/255.0, blue: 220/255.0, alpha: 1)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        tableView.mj_header = BLGifHeader(refreshingTarget: self, refreshingAction: #selector(NearTableViewController.getData))
    }
    
    func getData(){
        let maneger = AFHTTPSessionManager()
        maneger.responseSerializer = AFHTTPResponseSerializer()
        //var params= Dictionary<String,String>()
        
        maneger.GET("http://bbs.xuxian.com/?m=api&c=index&a=run&token=60090000829943&ver=2016040101", parameters: nil, progress: nil, success: { (_, JSON) in
            do{
                let temp = (try NSJSONSerialization.JSONObjectWithData(JSON as! NSData, options: NSJSONReadingOptions.MutableContainers) as! Dictionary<String,AnyObject>)["data"] as! Dictionary<String,AnyObject>
                let data = temp["forums"] as! Array<AnyObject>
                self.dataArray =  NearHomeModel.mj_objectArrayWithKeyValuesArray(data) as Array<AnyObject>
                self.tableView.reloadData()
                self.tableView.mj_header.endRefreshing()
            }catch{}
            
            
            }) { (_, error) in
                print(error)
                self.tableView.mj_header.endRefreshing()
        }
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let model = dataArray[indexPath.row] as! NearHomeModel
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! NearHomeTableViewCell
        cell.nearHomeModel = model
        cell.backgroundColor = UIColor.clearColor()
        return cell
    }
    
}
